package cn.com.customer.manager.ui.task

import android.annotation.SuppressLint
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import cn.com.customer.manager.R
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.NewStatusList
import cn.com.customer.manager.bean.RequestBean
import cn.com.customer.manager.bean.TaskInfo
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.NoTipRequestSubscriber
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import cn.com.customer.manager.manager.TokenManager
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.XHttpProxy
import com.xuexiang.xhttp2.annotation.ThreadType
import com.xuexiang.xhttp2.model.ApiResult
import com.xuexiang.xui.widget.dialog.DialogLoader
import kotlinx.android.synthetic.main.activity_task_detail.*

class TaskDetailActivity : BaseActivity() {

    var params: MutableMap<String, String> = mutableMapOf()
    var type: Int = 0
    lateinit var statResult: NewStatusList
    var statList: MutableList<String> = mutableListOf()
    override fun getLayoutId(): Int = R.layout.activity_task_detail
    @SuppressLint("CheckResult")
    override fun initView(savedInstanceState: Bundle?) {
        initActionBar(getString(R.string.task_detail_title))
        getTaskDetail()
        tdTask_state.setOnClickListener {
            showContextMenuDialog()
        }
        XHttpProxy.proxy(BaseApi.ITask::class.java, ThreadType.TO_IO)
                .taskStatusList(TokenManager.getInstance().token)
                .subscribeWith(object : NoTipRequestSubscriber<NewStatusList>() {
                    override fun onSuccess(t: NewStatusList?) {
                        t?.let { list ->
                            statResult = list
                            list.statusList.forEach {
                                statList.add(it.name)
                            }
                        }
                    }
                })

    }

    private fun getTaskDetail() {
        params["taskId"] = intent.getStringExtra("data")
        val request = XHttp.custom()
        request.call(request.create(BaseApi.ITask::class.java)
                .taskDetail(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<TaskInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<TaskInfo>?) {
                        apiResult?.let {
                            if (it.data != null) {
                                setData(it.data)
                            }
                        }
                    }
                })
    }

    /**
     * 类似系统的上下文菜单ContextMenu的Dialog
     */
    private fun showContextMenuDialog() {
        DialogLoader.getInstance().showContextMenuDialog(this,
                getString(R.string.tip),
                statList.toTypedArray(),
                DialogInterface.OnClickListener { dialog, which ->
                 modifyTaskState(statResult.statusList[which].code)
                })
    }

    private fun setData(data: TaskInfo) {
        tdTask_Type.text = data.taskType
        tdTask_Date.text = data.taskDate
        tdTask_customer.text = data.customerName
        tdTask_executeUser.text = data.executeUser
        tdTask_remind.text = data.remindWay
        tdTask_remark.text = data.remark
        tdTask_state.text = data.taskStatus
    }

    override fun onClick(v: View?) {
        super.onClick(v)


    }


    fun modifyTaskState(type: String) {
        params.clear()
        params["taskId"] = intent.getStringExtra("data")
        params["status"] = type
        val request = XHttp.custom()!!
        request.call(request.create(BaseApi.ITask::class.java)
                .taskModifyStatus(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<String>>() {
                    override fun onSuccess(apiResult: ApiResult<String>?) {
                        apiResult?.let {
                        }
                    }
                })

    }
}
